<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\OrderItem;
use App\Models\OrderRefund;
use App\Http\Controllers\Api\V1\Resources\OrderRefundResource;
use App\Http\Controllers\Api\V1\Requests\OrderRefundStoreRequest;
use App\Services\ApplyProductOrderRefund\ApplyProductOrderRefund;
use App\Http\Controllers\Api\V1\Resources\OrderRefundNegoResource;
use App\Services\ApplyProductOrderRefund\ApplyProductOrderRefundRequest;

class OrderRefundsController extends Controller
{
	public function show(OrderItem $item)
	{
		$refund = $item->refund;
		return $refund ? new OrderRefundResource($refund) : null;
	}

	public function reasons()
	{
		return response()->json(['data' => config('site.order.refund.reasons')]);
	}

	public function store(ApplyProductOrderRefundRequest $request)
	{
		$refund = app(ApplyProductOrderRefund::class, [
			'item' => $request->getItem(),
			'type' => $request->type,
			'reason' => $request->reason,
			'parcelStatus' => $request->parcel_status,
			'refundMoney' => $request->refund_money,
			'nego_content' => $request->nego_content,
			'proof_images' => $request->proof_images
		])->apply();

		return new OrderRefundResource($refund);
	}

	public function negos(OrderRefund $orderRefund)
	{
		$negos = $orderRefund->orderRefundNegos()->orderByDesc('id')->paginate(50);
		return OrderRefundNegoResource::collection($negos);
	}
}
